<?php
/**
 * wap2.0 用户中心－地址编辑保存
 */

include_once('includes/cls_json.php');
$json = new JSON;
$result = array('error' => 0, 'message' => '', 'url'=>'');

//var_dump($_POST);exit;

// 地址添加修改类型
$filter	= empty($_REQUEST['filter']) ? '' : $_REQUEST['filter'];

$address_id = empty($_REQUEST['address_id']) ? 0 : $_REQUEST['address_id'];
// 收货人
$consignee 	= empty($_REQUEST['consignee']) ? '' : trim($_REQUEST['consignee']);
// 省
$province 	= empty($_REQUEST['province']) ? 0 : $_REQUEST['province'];
// 市
$city 		= empty($_REQUEST['city']) ? 0 : $_REQUEST['city'];
// 区
$district 	= empty($_REQUEST['district']) ? 0 : $_REQUEST['district'];
// 街道
$street 	= empty($_REQUEST['street']) ? 0 : $_REQUEST['street'];
// 邮编
$postcode 	= empty($_REQUEST['postcode']) ? '' : trim($_REQUEST['postcode']);
// 详细地址
$address 	= empty($_REQUEST['address']) ? '' : trim($_REQUEST['address']);
// 手机
$mobile 	= empty($_REQUEST['mobile']) ? '' : trim($_REQUEST['mobile']);
// 电话
$tel 		= empty($_REQUEST['tel']) ? '' : trim($_REQUEST['tel']);
// flag
$flag 		= trim($_REQUEST['flag']);

$back_act 	= empty($_COOKIE['WAP']['back_act']) ? '' : urldecode($_COOKIE['WAP']['back_act']);
if (empty($back_act))
{
	$back_act = 'index.php?m=user&act=address';
}


/* 输入检查 */
if (empty($consignee)){
	$result = array('error' => 1, 'message' => '请填写收货人');
}
else if (empty($province)){
	$result = array('error' => 1, 'message' => '请选择省');
}
else if (empty($city)){
	$result = array('error' => 1, 'message' => '请选择市');
}
else if (empty($district)){
	$result = array('error' => 1, 'message' => '请选择区');
}
/*else if (empty($street)){
 $result = array('error' => 1, 'message' => '请选择街道');
 }*/
else if (empty($address)){
	$result = array('error' => 1, 'message' => '请填写收货地址');
}
else if (empty($postcode)){
	$result = array('error' => 1, 'message' => '请填写邮编');
}
else if (empty($mobile) && empty($tel)){
	$result = array('error' => 1, 'message' => '手机和电话必须填一项');
}
else if ($mobile && !preg_match("/^1[3458][0-9]{9}$/", $mobile)){
	$result = array('error' => 1, 'message' => '手机格式错误');
}
else if ($tel && !preg_match("/^0[0-9]{2,3}[-]?\d{7,8}$/", $tel)){
	$result = array('error' => 1, 'message' => '电话格式错误，正确格式为：区号固话号码');
}

// 错误提示
if ($result['error'])
{
	die($json->encode($result));
}

// 检查登录
if ( empty($user_token) )
{
	$result = array('error' => -1, 'message' => '', 'url'=>'user-login.html');
	die($json->encode($result));
}

// 兼容部分城市区无街道信息，则使用区
if (empty($street))
{
	$street = $district;
}

// 新增地址
if ($filter == 'add')
{
	$result = $mInterface->addUserAddress($user_token, $consignee, $province, $city, $district, $address, $mobile, $tel, $postcode);

	if ($result['status'] && is_array($result['result'])){
		$result = $result['result'];
	}else{
		$result = array();
	}

	if($flag == '1' && $result['id']){
		//wap_setcookie("WAP[update_address_id]", $result['id']);
	}

	if (empty($result) || !$result['id'])
	{
		$result = array('error' => -1, 'message' => '', 'url'=>'');
	}
	else
	{
		$result = array('error' => -1, 'message' => '', 'url'=>$back_act);
	}
}
// 修改地址
else if ($filter == 'update' && $address_id)
{
	if($flag=='1'){
		wap_setcookie("WAP[update_address_id]",$address_id);
	}
	$result = $mInterface->updateUserAddress($user_token, $address_id, $consignee, $province, $city, $street, $address, $mobile, $tel, $postcode);
exit;
	if ($result['status'] && is_array($result['result'])){
		$result = array('error' => -1, 'message' => '', 'url'=>$back_act);
	}else{
		$result = array('error' => -0, 'message' => $result, 'url'=>$back_act);
	}
}

// 返回结果
die($json->encode($result));